import java.util.*;

public class day230506 {
    // pre-post
    private static long calcTreePossible(int m, String pre, String post) {
        if (pre.length() <= 1) {
            return 1;
        }
        // 计算结果
        long result = 1;
        // 计算子树个数
        int count = 0;

        // 子树根结点, 在前序中的下标
        int preRoot = 1;
        // 后序中, 子树的起始位置
        int postFirst = 0;

        while (preRoot < pre.length()) {
            // 子树个数加一
            count++;

            // 子树根结点, 在后序遍历中的下标
            int postRoot = post.indexOf(pre.charAt(preRoot));

            // 这颗子树的节点数
            int treeNodeCount = postRoot - postFirst + 1;

            // 递归这颗子树
            String s1 = pre.substring(preRoot, preRoot + treeNodeCount);
            String s2 = post.substring(postFirst, postRoot + 1);
            result *= calcTreePossible(m, s1, s2);

            preRoot += treeNodeCount;
            postFirst += treeNodeCount;
        }

        // 通过子树个数, 计算当前根结点, 子树的可能位置
        // c(3, 13) = c(10, 13)  计算遍历次数少的
        count = Math.min(count, (m - count));

        // 分子
        int x1 = 1;
        for (int i = 1; i <= count; i++) {
            x1 *= m;
            m--;
        }
        // 分母
        int x2 = 1;
        for (int i = 1; i <= count; i++) {
            x2 *= i;
        }

        result *= x1 / x2;

        return result;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            int m = sc.nextInt();
            if (m == 0) {
                // 输入0退出
                break;
            }

            // 接收前序和后序遍历字符串
            String pre = sc.next();
            String post = sc.next();

            System.out.println(calcTreePossible(m, pre, post));
        }
    }


    // Rational Arithmetic
    // 有理算术
    public static void main1(String[] args)  {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.next();
        String s2 = sc.next();
        // 拿到 分子 和 分母
        long a1 = Long.parseLong(s1.split("/")[0]);
        long b1 = Long.parseLong(s1.split("/")[1]);
        long a2 = Long.parseLong(s2.split("/")[0]);
        long b2 = Long.parseLong(s2.split("/")[1]);

        System.out.println(print(a1, b1) + " + " + print(a2, b2) + " = " + addAndPrint(a1, b1, a2, b2));
        System.out.println(print(a1, b1) + " - " + print(a2, b2) + " = " + subAndPrint(a1, b1, a2, b2));
        System.out.println(print(a1, b1) + " * " + print(a2, b2) + " = " + mulAndPrint(a1, b1, a2, b2));
        System.out.println(print(a1, b1) + " / " + print(a2, b2) + " = " + divAndPrint(a1, b1, a2, b2));
    }

    // 给定 5/3 , 返回字符串 1 2/3
    private static String print(long a, long b) {
        // 分母为0
        if (b == 0) {
            return "Inf";
        }
        // 分子为 0
        if (a == 0) {
            return "0";
        }
        StringBuilder sb = new StringBuilder();
        // 分子或分母有一个是负数
        if (a < 0 || b < 0) {
            sb.append("(-");
        }

        // 将分数化简为
        // 整数部分 + 最简分数
        long integer = a / b;
        long a1 = Math.abs(a - integer * b);
        long b1 = Math.abs(b);

        // 整数算完分子后, 改为正数
        integer = Math.abs(integer);

        long gcd = gcd(a1, b1);
        a1 = a1 / gcd;
        b1 = b1 / gcd;

        if (integer != 0) {
            // 整数不为0
            sb.append(integer);
            if (a1 != 0) {
                // 分数不为0, 加个空格
                sb.append(" ");
            }
        }

        if (a1 != 0) {
            // 分数不为 0
            sb.append(a1);
            sb.append("/");
            sb.append(b1);
        }

        if (a < 0 || b < 0) {
            sb.append(")");
        }

        return sb.toString();
    }

    // 求最大公约数
    private static long gcd(long a, long b) {
        while (b != 0) {
            long tmp = a % b;
            a = b;
            b = tmp;
        }
        return a;
    }

    // 相加并且打印
    private static String addAndPrint(long a1, long b1, long a2, long b2) {
        long a = a1 * b2 + a2 * b1;
        long b = b1 * b2;
        return print(a, b);
    }
    // 相减并且打印
    private static String subAndPrint(long a1, long b1, long a2, long b2) {
        long a = a1 * b2 - a2 * b1;
        long b = b1 * b2;
        return print(a, b);
    }
    // 相乘并且打印
    private static String mulAndPrint(long a1, long b1, long a2, long b2) {
        long a = a1 * a2;
        long b = b1 * b2;
        return print(a, b);
    }
    // 相除并且打印
    private static String divAndPrint(long a1, long b1, long a2, long b2) {
        long a = a1 * b2;
        long b = a2 * b1;
        return print(a, b);
    }
}
